<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Outreq"%>
<%@page import="db.OutreqDAO"%>
<%@page import="db.OutpurmatDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.Outpurmat"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.Receivepurmat"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<% 
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	OutreqDAO outreqDao = (OutreqDAO)ctx.getBean("OutreqDAO");
	OutpurmatDAO outpurmatDao = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	
	String reqCode = request.getParameter("reqCode");
	String reqDate = request.getParameter("reqDate");
	String tobId = request.getParameter("tobId");
	String projectId = request.getParameter("projectId");
	String reccompanyId = request.getParameter("reccompanyId");
	String companyId = request.getParameter("companyId");
	String remark = request.getParameter("remark");
	String maReqUserId = request.getParameter("maReqUserId");
	
	
	int uid = (Integer)session.getAttribute("id");
	int tid = (Integer)session.getAttribute("tid");
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	Outreq outreq = new Outreq();
	try{
		outreq.setCompanyId(Integer.parseInt(companyId));
	}catch(Exception e){
		outreq.setCompanyId(0);
	}
	outreq.setReqDate(df.parse(reqDate));
	try{
		outreq.setTobId(Integer.parseInt(tobId));
	}catch(Exception e){
		outreq.setTobId(0);
	}
	try{
		outreq.setProId(Integer.parseInt(projectId));
	}catch(Exception e){
		outreq.setProId(0);
	}
	try{
		outreq.setReccompanyId(Integer.parseInt(reccompanyId));
	}catch(Exception e){
		outreq.setReccompanyId(0);
	}
	outreq.setRemark(remark);
	outreq.setTeamId(tid);
	outreq.setPhase(2);
	outreq.setMaReqState(1);
	outreq.setReqUserId(uid);
	outreq.setReqUserDate(new Date());
	if(maReqUserId != null && !"".equals(maReqUserId.trim())){
		outreq.setMaReqUserId(Integer.parseInt(maReqUserId));
	}
	outreqDao.save(outreq);
	
	outreq.setOutCode("Q"+outreq.getId());
	outreqDao.attachDirty(outreq);
	
	String[] materialIds = request.getParameterValues("materialIds");
    String[] taskCodes = request.getParameterValues("taskCodes");
    String[] reqNums = request.getParameterValues("reqNums");
    
    for(int i=0;i<materialIds.length;i++){
    	Outpurmat outpurmat = new Outpurmat();
    	outpurmat.setDate(new Date());
    	outpurmat.setOutReqId(outreq.getId());
    	outpurmat.setReceivePurmatId(Integer.parseInt(materialIds[i]));
    	outpurmat.setReqNum(Integer.parseInt(reqNums[i]));
    	outpurmat.setState(1);
    	outpurmat.setTaskCode(taskCodes[i]);
    	outpurmatDao.save(outpurmat);
    	Receivepurmat receivepurmat = receivepurmatDao.findById(Integer.parseInt(materialIds[i]));
    	receivepurmat.setUseNum(receivepurmat.getUseNum() - Integer.parseInt(reqNums[i]));
    	receivepurmatDao.attachDirty(receivepurmat);
    }
    
    request.setAttribute("outCode",outreq.getOutCode());
    request.setAttribute("outId", outreq.getId());
    request.setAttribute("info","success");
    pageContext.forward("/vm/outStock/stockApp.vm");
%>